package com.mis.pos.repository;

import com.mis.pos.entity.TypeEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Description 类别表的DAO
 * @Author yuxiang
 * @CreatedDate 2019/10/30 17:57
 */
@Repository
public interface TypeRepository extends JpaRepository<TypeEntity,Long>, JpaSpecificationExecutor<TypeEntity> {

    /**
     * 根据id查询实体
     * @param id
     * @return
     */
    TypeEntity findClassificationEntityById(Long id);

    /**
     * 根据分类名称来查询分类实体
     * @param className
     * @return
     */
    TypeEntity findClassificationEntityByClassNameOrClassNumber(@Param("className") String className, @Param("classNumber") String classNumber);

    /**
     * 根据id删除
     * @param id
     */
    void deleteById(Long id);

    /**
     * 根据id更改分类信息
     * @param id 类别实体id
     * @param className
     * @param classNumber
     */
    @Transactional
    @Modifying
    @Query(value = "update TypeEntity ct set ct.classNumber=:classNumber,ct.className=:className where ct.id=:id")
    void updateClassificationEntity(@Param("id") Long id,@Param("className")String className,@Param("classNumber")String classNumber);

    /**
     * 查询全部分类名称
     * @return
     */
    @Query(value = "select ct.class_name from type ct",nativeQuery = true)
    List<String> findAllClassName();


}
